package com.william.jdbcplus.core.dialect.fill;

import com.william.jdbcplus.common.enums.IdType;
import com.william.jdbcplus.core.dialect.fill.impl.*;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 主键填充工厂
 *
 * @author william
 */
public class KeyFactory {

    /**
     * 储存反射类表信息
     */
    private static final Map<IdType, IFill> KEY_INFO_CACHE = new ConcurrentHashMap<>();

    static {
        for (IdType idType : IdType.values()) {
            KEY_INFO_CACHE.put(idType, getFillByType(idType));
        }
    }

    /**
     * @param idType id生成类型
     */
    private static IFill getFillByType(IdType idType) {
        switch (idType) {
            case ATOMIC:
                return new AutomicFill();
            case NUMBER:
                return new NumberFill();
            case GUID:
                return new GuidFill();
            case SNOWFLAKE:
                return new SnowFlakeFill();
            case SEQUENCE:
                return new SequenceFill();
            default:
                return new GuidFill();
        }
    }

    public static IFill getInstance(IdType dbType) {
        IFill fill = KEY_INFO_CACHE.get(dbType);
        return fill;
    }
}
